import { Util } from '../../utils/util';
import { Engine } from '../engine';
import { Store } from '../store/store';
import { FundAccountInsideType } from '../common/fundDefinition';
import { SelfDefFundAccountType } from '../funds/selfDefFundAccountType';

/**
 * 资金账户过滤条件
 */
export class FundAccountFilter {
    /**
     * 门店过滤
     * @type {string|null}
     */
    storeId;

    // 账户类型  FundAccountInsideType
    accountInsideType;

    /**
     * 构造含税
     * @param {Object} options
     * @param {string|null} [options.storeId] - 门店ID
     */
    constructor({ storeId = null,likeName,accountInsideType = null } = {}) {
        this.storeId = storeId ?? Engine.profile?.filterStore?.id ?? null;
        this.likeName = likeName;
        this.accountInsideType = accountInsideType;
    }

    /**
     * 克隆构造函数
     * @param {FundAccountFilter} filter - 要克隆的过滤器对象
     * @returns {FundAccountFilter}
     */
    static clone(filter) {
        return new FundAccountFilter({
            storeId: filter.storeId
        });
    }

    /**
     * 转换为JSON对象
     * @returns {Object} JSON representation of the filter
     */
    _$FundAccountFilterToJson() {
        const ret = {};

        // 门店过滤
        if (this.storeId != null) {
            ret["storeId"] = this.storeId;
        }

         if (this.likeName) {
            ret.lke_name = this.likeName;
        }

        if(this.accountInsideType != null){
            ret.accountType = this.accountInsideType.code;
        }

        return ret;
    }

    /**
     * 转换为JSON对象
     * @returns {Object} JSON representation of the filter
     */
    toJson() {
        return this._$FundAccountFilterToJson();
    }
}

export class FundAccount {
    constructor({
        id,
        name,
        store = null,
        accountNo = '',
        accountCode = '',
        balance,
        income,
        expenses,
        bankAccountName = '',
        bankName = '',
        remark = '',
        accountType = FundAccountInsideType.NormalAccount,
        selfDefFundAccountType  // SelfDefFundAccountType 自定义资金类型，如黄金，白银
    }={}) {
        this.id = id;
        this.name = name;
        this.accountNo = accountNo;
        this.accountCode = accountCode;
        this.balance = balance;
        this.income = income;
        this.expenses = expenses;
        this.bankAccountName = bankAccountName;
        this.bankName = bankName;
        this.remark = remark;
        this.accountType = accountType;
        this.store = store ?? Engine.profile?.getValideStore ?? null;
        this.selfDefFundAccountType = selfDefFundAccountType;
    }

    static empty({
        id = '',
        name = '',
        store = null,
        accountNo = '',
        accountCode = '',
        balance = 0,
        income = 0,
        expenses = 0,
        bankAccountName = '',
        bankName = '',
        remark = '',
        accountType = FundAccountInsideType.NormalAccount
    } = {}) {
        return new FundAccount({
            id,
            name,
            store: store ?? Engine.profile?.getValideStore,
            accountNo,
            accountCode,
            balance,
            income,
            expenses,
            bankAccountName,
            bankName,
            remark,
            accountType
        });
    }

    static clone(data) {
        return new FundAccount({
            id: data.id,
            name: data.name,
            accountNo: data.accountNo,
            accountCode: data.accountCode,
            balance: data.balance,
            income: data.income,
            expenses: data.expenses,
            bankAccountName: data.bankAccountName,
            bankName: data.bankName,
            remark: data.remark,
            accountType: data.accountType,
            store: data.store
        });
    }

    static fromJson(json) {
        return new FundAccount({
            id: json.id ? json.id.toString() : '',
            name: json.name ?? '总账',
            accountNo: json.accountNo ?? '',
            balance: json.moneys ?? 0,
            income: json.totalIncom ?? 0,
            expenses: json.totalOut ?? 0,
            bankAccountName: Util.jsonToString(json.bankAccountName),
            bankName: Util.jsonToString(json.bankName),
            remark: Util.jsonToString(json.remark),
            accountCode: Util.jsonToString(json.accountCode),
            accountType: Util.jsonToFundAccountInsideType(
                json.accountType,
                FundAccountInsideType.NormalAccount
            ),
            store: json.storeId ? Store.fromJsonForRelatedSource(json) : null,            
            selfDefFundAccountType: json.metalAccountTypeId == null ? null : new SelfDefFundAccountType(Util.jsonToString(json.metalAccountTypeId))

        });
    }

    toJson() {
        const ret = {};

        if (this.id && this.id !== '') {
            ret.id = this.id;
        }
        ret.name = this.name;
        ret.accountNo = this.accountNo;
        ret.bankAccountName = this.bankAccountName;
        ret.bankName = this.bankName;
        ret.remark = this.remark;
        ret.accountCode = this.accountCode;

        // 新建时设置初始余额,编辑现有账号不可修改余额
        if (Util.isEmpty(this.id)) {
            ret.moneys = this.balance;
        }

        ret.belongerId = Engine.profile?.org?.orgId ?? '';
        ret.accountType = this.accountType.code;  // 注意测试

        if (this.store) {
            ret.storeId = this.store.id;
        }

        return ret;
    }

    // 拷贝传参用，与 fromJson 一一对应
    toCopyJson() {
        let ret = {
            id: this.id,
            name: this.name,
            accountNo: this.accountNo,
            moneys: this.balance.value, // matches balance in fromJson
            totalIncom: this.income.value, // matches income in fromJson
            totalOut: this.expenses.value, // matches expenses in fromJson
            bankAccountName: this.bankAccountName,
            bankName: this.bankName,
            remark: this.remark,
            accountCode: this.accountCode,
            accountType: this.accountType.code, // matches Util.jsonToFundAccountInsideType in fromJson
            storeId: this.store?.id, // matches Store.fromJsonForRelatedSource in fromJson            
        };

        let storeJson = this.store?.toCopyJsonForRelatedSource();

        if (storeJson) {
            // 合并 ret 和 storeJson
            ret = { ...ret, ...storeJson };
        }

        return ret;
    }
}
